/**
 * Copyright (c) 2016-2019 九点科技 All rights reserved.
 *
 * http://www.9dyun.cn
 *
 * 版权所有，侵权必究！
 */
package com.dian.modules.base.service.impl;

import cn.hutool.core.util.StrUtil;
import com.dian.common.validator.ValidatorUtils;
import com.dian.common.validator.group.AddGroup;
import com.dian.common.validator.group.UpdateGroup;
import com.dian.modules.base.entity.ClassEntity;
import com.dian.modules.base.service.ClassService;
import  com.dian.common.enums.ValidEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import com.dian.common.utils.PageUtils;
import com.dian.common.utils.Query;
import com.dian.common.server.CommonService;
import com.dian.modules.base.dao.GoodsDao;
import com.dian.modules.base.entity.GoodsEntity;
import com.dian.modules.base.service.GoodsService;

/**
 * 商品资料表服务实现类
 *
 * @author xiaolai
 * @email xiaolai@9dyun.cn
 * @date 2019-06-10 19:06:22
 */
@Service("goodsService")
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, GoodsEntity> implements GoodsService {

    @Autowired
    public CommonService commonService;

    @Autowired
    public ClassService classService;

    /**
     * 按商品编码查询
     * @param goodsCode
     * @return
     */
    @Override
    public GoodsEntity queryByCode(String goodsCode) {
        QueryWrapper<GoodsEntity> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("goods_code",goodsCode);
        queryWrapper.eq("is_valid", ValidEnum.YES.getValue());
        GoodsEntity goodsEntity =this.getOne(queryWrapper);
        return goodsEntity;
    }

    /**
     * 商品资料表分页
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        IPage<GoodsEntity> page = this.page(new Query<GoodsEntity>().getPage(params),getQueryWrapper(params) );
        return new PageUtils(page);
    }

    /**
     *  商品资料表新增
     * @param goodsEntity
     * @return
     */
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public boolean saveInfo(GoodsEntity goodsEntity) {

        goodsEntity.setGoodsCode(commonService.getCode(GoodsEntity.class));
        //数据完整性校验
        this.paramsCheck(goodsEntity,AddGroup.class);

        //设置编码，基础默然初始化数据设置
        //goodsEntity.setOrderNo(commonService.getOrderCode("XSD"));
        //goodsEntity.setOrderDate(new Date());


        //保存
        this.save(goodsEntity);

        return true;
    }

    /**
     *商品资料表更新
     * @param goodsEntity
     * @return
     */
    @Override
    public boolean updateInfo(GoodsEntity goodsEntity) {

        //修改状态校验
        this.updateCheck(goodsEntity.getId());

        //主表数据完整性校验
        this.paramsCheck(goodsEntity, UpdateGroup.class);

        //更新主表
        this.updateById(goodsEntity);


        return true;
    }

    /**
     *商品资料表删除
     * @param id
     * @return
     */
    @Override
    public boolean deleteInfo(Long id) {

        //删除状态校验
        this.deleteCheck(id);

        //更新主表
        this.removeById(id);

        return true;
    }

    /**
     * 商品资料表详情
     * @param id
     * @return
     */
    @Override
    public GoodsEntity getInfo(Long id) {
        GoodsEntity goodsEntity =getById(id);
        return goodsEntity;
    }

    /**
     * 商品资料表审核
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Throwable.class)
    public boolean checkInfo(Long id) {
        GoodsEntity goodsEntity =this.getById(id);
        checkCheck(goodsEntity);
        //saleOrderHeadEntity.setOrderState(OrderHead_OrderStateEnum.AUDITED.getValue());
        //saleOrderHeadEntity.setOrderDate(new Date());
        ///saleOrderHeadEntity.setCheckUserId(commonService.getUserId());
        //saleOrderHeadEntity.setCheckUserName(commonService.getUserName());
        return this.updateById(goodsEntity);
    }




    /***********************************************************************************************/
    /****************************************** 私有方法 ********************************************/
    /***********************************************************************************************/

    /**
     * 修改状态校验
     *
     * @param
     */
    private void updateCheck(Long id) {
        GoodsEntity goodsEntity =this.getById(id);
        //if(goodsEntity.getOrderState().equals(OrderHead_OrderStateEnum.AUDITED.getValue())){
        //    throw new RRException(String.format("已审核的销售订单禁止修改"));
        //}
    }
    /**
     * 审核状态校验
     *
     * @param
     */
    private void checkCheck(GoodsEntity goodsEntity) {
        //if(goodsEntity.getOrderState().equals(OrderHead_OrderStateEnum.AUDITED.getValue())){
        //    throw new RRException(String.format("[%s] 此销售单已审核"));
        //}
    }

    /**
     * 删除状态校验
     *
     * @param id
     */
    private void deleteCheck(Long id) {
        GoodsEntity goodsEntity = this.getById(id);
    }

    /**
     * 新增和修改参数校验
     *
     * @param goodsEntity
     */
    private void paramsCheck(GoodsEntity goodsEntity, Class<?> cls) {

        //数据完整性校验
        if(goodsEntity.getClassId()==0L){
            goodsEntity.setClassName("/0/");
            goodsEntity.setClassName("一级分类");
            goodsEntity.setClassPathId("/0/");
            goodsEntity.setClassPathName("/一级分类/");
        }else {
            ClassEntity pClassEntity=classService.getById(goodsEntity.getClassId());
            goodsEntity.setClassPathName(pClassEntity.getPathName()+pClassEntity.getClassName()+"/");
            goodsEntity.setClassPathId(pClassEntity.getPathId()+pClassEntity.getId()+"/");
            goodsEntity.setClassName(pClassEntity.getClassName());
            goodsEntity.setClassId(pClassEntity.getId());
        }

        ValidatorUtils.validateEntity(goodsEntity, cls);
    }
    /**
     * 获取查询条件
     *
     * @param
     */
    private QueryWrapper<GoodsEntity> getQueryWrapper(Map<String, Object> params) {
        QueryWrapper<GoodsEntity> queryWrapper=new QueryWrapper<>();
        if(!StrUtil.isBlankIfStr(params.get("goodsCode"))){
            queryWrapper.likeLeft("goods_code",params.get("goodsCode"));
        }
        if(!StrUtil.isBlankIfStr(params.get("goodsName"))){
            queryWrapper.likeLeft("goods_name",params.get("goodsName"));
        }
        queryWrapper.orderByAsc("id");
        return queryWrapper;
    }
}
